using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScottIsAFool.Common;

namespace InsertVideo
{
    public partial class frmMain : Form
    {
        AssemblyInfo ai = new AssemblyInfo();
        VideoSettings _settings;
        GetVideoCode vc;
        string code = "";
        string novideo = "<html><body><p>No video loaded</p></body></html>";
        bool pluginisfine = false;


        public frmMain(VideoSettings settings)
        {
            InitializeComponent();
            _settings = settings;
            setSettings();
            this.Text = string.Format("{0} v{1}", ai.AssemblyTitle, ai.AssemblyVersion.Substring(0, 5));
            lblTitle.Text = ai.AssemblyProduct;
            lblCopy.Text = ai.AssemblyCopyright;
            vc = new GetVideoCode(_settings.IsEnabled, _settings.DefaultWidth);
            browser.DocumentText = novideo;
        }

        private void setSettings()
        {
            int option = _settings.DefaultOption;
            cbOptions.SelectedIndex = option;
            chPreview.Checked = _settings.Preview;
            chOpenNewWindow.Checked = _settings.OpenNew;
            chTransparent.Checked = _settings.Transparency;
            chTitle.Checked = _settings.TitleIsEnabled;
            chAutoplay.Checked = _settings.Autoplay;
            chYouTube.Checked = _settings.YouTubeRelative;
        }

        private void linkFeedback_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "mailto:wlm@scottisafool.co.uk?Subject=Feedback from Video Plugin";
            process.Start();

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "http://scottisafooldev.spaces.live.com/";
            process.Start();
        }

        private void txbxLink_TextChanged(object sender, EventArgs e)
        {
            if (txbxLink.Text != "")
            {
                string vp = vc.VideoProvider(txbxLink.Text);
                if (vp == "na")
                {
                    grpOptions.Enabled = false;
                    lblVideoSize.Visible = false;
                    MessageBox.Show("You got this message because:\n1) You tried typing the link (just copy and paste);\n2) The link is invalid, or;\n3) The video site isn't supported yet. If you would like this site to be added, email wlm@scottisafool.co.uk with the details.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (vp == "muveemix")
                    {
                        chTitle.Enabled = false;
                        chTitle.Checked = false;
                    }
                    else
                        chTitle.Enabled = true;

                    if (vp == "youtube")
                    {
                        grpOptions.Size = new System.Drawing.Size(209, 206);
                    }
                    else
                    {
                        grpOptions.Size = new System.Drawing.Size(209, 176);
                    }
                    if (!vc.AllowAutoPlay)
                    {
                        chAutoplay.Enabled = vc.AllowAutoPlay;
                        chAutoplay.Checked = vc.AllowAutoPlay;
                    }
                    grpOptions.Enabled = true;
                    chPreview_CheckedChanged(this, e);
                    pluginisfine = true;
                    code = vc.Code;
                    lblVideoSize.Text = string.Format("Video size: {0}x{1}", vc.GetWidth.ToString(), vc.GetHeight.ToString());
                    lblVideoSize.Visible = true;
                }
            }
            else
            {
                grpOptions.Enabled = false;
                lblVideoSize.Visible = false;
                browser.DocumentText = novideo;
            }
        }

        private void chPreview_CheckedChanged(object sender, EventArgs e)
        {
            string html;

            if (chPreview.Checked == true)
            {
                html = "<html><body style=\"margin: 0px;\">";
                html += string.Format(vc.Code, browser.Width.ToString(), browser.Height.ToString(), vc.GetID, " wmode=\"transparent\"", "", "");
                html += "</body></html>";
            }
            else
            {
                html = novideo;
            }
            browser.DocumentText = html;

        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (pluginisfine)
            {
                _settings.Embed = code;
                _settings.Option = cbOptions.SelectedIndex;
                _settings.Width = vc.GetWidth;
                _settings.Link = txbxLink.Text;
                _settings.Provider = vc.GetProvider;
                _settings.Id = vc.GetID;
                _settings.Ratio = vc.GetRatio;
                _settings.OpenNew = chOpenNewWindow.Checked;
                _settings.Transparency = chTransparent.Checked;
                _settings.Title = vc.GetHtmlPageSource;
                _settings.TitleIsEnabled = chTitle.Checked;
                _settings.Autoplay = chAutoplay.Checked;
                _settings.AutoPlayValue = vc.AutoPlay;
                _settings.AllowAutoPlay = vc.AllowAutoPlay;
                _settings.YouTubeRelative = chYouTube.Checked;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Sorry, you haven't entered a valid video link, please try again.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void cbOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbOptions.SelectedIndex)
            {
                case 0:
                    chOpenNewWindow.Enabled = false;
                    chAutoplay.Enabled = true;
                    chTransparent.Enabled = true;
                    chTitle.Enabled = true;
                    chYouTube.Enabled = true;
                    break;
                case 1:
                    chOpenNewWindow.Enabled = true;
                    chAutoplay.Enabled = false;
                    chTransparent.Enabled = false;
                    chTitle.Enabled = true;
                    chYouTube.Enabled = false;
                    break;
                case 2:
                    chOpenNewWindow.Enabled = true;
                    chAutoplay.Enabled = true;
                    chTransparent.Enabled = true;
                    chTitle.Enabled = true;
                    chYouTube.Enabled = true;
                    break;
            }
        }

        private void linkExamples_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmHelp help = new frmHelp();
            help.ShowDialog();
        }

    }
}